/*
    Speedbump aka Project Kimbula
    Copyright (C) 2010  Ping Shin Ching

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see http://www.gnu.org/licenses.

	Ping Shin Ching
	righteous.ninja@gmail.com OR ping_sc@hotmail.com
	More information at http://www.tidytutorials.com/p/speedbump.html
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "application.h"
#include "helpers.h"

/*************************************************************************
 * stristr
 *************************************************************************/
char* stristr(const char* haystack, const char* needle){
	int i,j;
	short quit=1;
	int diff;
	
	if(!haystack || !needle || (diff=strlen(haystack)-strlen(needle))<0){
		return 0;
	}

	for(i=0;i<=diff;i++){
		quit=0;
		for(j=0;j<strlen(needle);j++){
			if(toupper(needle[j])!=toupper(haystack[i+j])){
				quit=1;
				break;
			}
		}
		if(!quit){
			return (char*)&haystack[i];
		}
	}
	return 0;
}


/*************************************************************************
 * stristr
 *************************************************************************/
char* strnstrn(const char* haystack, const unsigned int haystacksize, const char* needle, const unsigned int needlesize){
	int i,j;
	short quit=1;
	int diff;

	if(!haystack || !needle || (diff=haystacksize-needlesize)<0){
		return 0;
	}

	for(i=0;i<=diff;i++){
		quit=0;
		for(j=0;j<needlesize;j++){
			if(needle[j]!=haystack[i+j]){
				quit=1;
				break;
			}
		}
		if(!quit){
			return (char*)&haystack[i];
		}
	}
	return 0;
}

/*	Subtract the `struct timeval' values X and Y,
	storing the result in RESULT.
	Return 1 if the difference is negative, otherwise 0.  */
     
 int timeval_subtract(timeval* result, timeval* x, timeval* y){
	/* Perform the carry for the later subtraction by updating y. */
	if (x->tv_usec < y->tv_usec) {
		int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
		y->tv_usec -= 1000000 * nsec;
		y->tv_sec += nsec;
	}
	if (x->tv_usec - y->tv_usec > 1000000) {
		int nsec = (x->tv_usec - y->tv_usec) / 1000000;
		y->tv_usec += 1000000 * nsec;
		y->tv_sec -= nsec;
	}

	/* Compute the time remaining to wait.
	tv_usec is certainly positive. */
	result->tv_sec = x->tv_sec - y->tv_sec;
	result->tv_usec = x->tv_usec - y->tv_usec;
     
	/* Return 1 if result is negative. */
	return x->tv_sec < y->tv_sec;
}

void printchararray(const char* a, unsigned int s, const char* heading){
	printf("__________%s (%d)___________\n", heading, s);
	for(int i=0;i<s;i++)
		printf("%c", a[i]);
	printf("__________\n");
}


/*
 * Handle null terminated arrrays
 */
int ntarrayadd(void*** p, int sizeofpointer, void* pvalue, int sizeofvalue){
        int i;
        void** pv;
        if(*p==0){
                *p=(void**)malloc(sizeofpointer);
                (*p)[0]=0;
        }
        for(i=0; (*p)[i]!=0;i++);
        if((pv=(void**)realloc(*p, sizeofpointer*(i+2)))){
                (*p)=pv;
                (*p)[i]= malloc(sizeofvalue);
                memcpy((*p)[i], pvalue, sizeofvalue);
                (*p)[i+1]=0;
                return (i+1);
        }
        else
                return 0;

}
void ntarraydestroy(void***p){
        int i;
        for(i=0;(*p)[i]!=0;i++){
                free((*p)[i]);
        }
        free(*p);
}

/*
 * Generate a unique id from the ipv6/4 address
 */
static char* gen_session_id_chars [2] = {"abcdefABCDEF1234567890.:", "FEDCBA0987654321GHIJKLMNOPQRSTUVWXYZ"};
int generate_session_id(const char* param1, char* out, int outsize){
	const char* x;
	int i;

	if(outsize <strlen(param1)+1){
		return -1;
	}
	else{
		memset(out, 0, outsize);
		i=0;
		for(x=param1 ; *x!= '\0' ; x++){
			out[i++]=gen_session_id_chars[1][strchr(gen_session_id_chars [0], *x)-gen_session_id_chars [0]];
		}
		out[i]='\0';
		return 0;
	}
}